﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;

#endregion

namespace Weazel.Gezel.Parser
{
  /// <summary>
  /// The parse method in this class parses a comma seperated identifer list
  /// </summary>
  public static class ParseList
  {
    public static readonly Set<Token> First = new Set<Token>(Token.Identifier);

    /*
    /// <summary>
    /// This method parses a comma seperated identifer list
    /// </summary>
    /// <param name="follow">Terminals to follow</param>
    /// <param name="t">The current tokenizer</param>
    /// <returns>List of identifiers parsed</returns>
    public static List<string> Parse(Set<Token> follow, Tokenizer t)
    {
      List<Model.Position> notUsed = new List<Model.Position>();
      return Parse(follow, t, ref notUsed);
    }
     */

    /// <summary>
    /// This method parses a comma seperated identifer list
    /// </summary>
    /// <param name="follow">Terminals to follow</param>
    /// <param name="t">The current tokenizer</param>
    /// <param name="position">List to hold position information of parsed indetifiers</param>
    /// <returns>list of identifers parsed</returns>
    public static List<string> Parse(Set<Token> follow, Tokenizer t, ref List<Gezel.Model.CompoundPosition> position)
    {
      ParserDebug.WriteLine(
        "Entering 'ParseList'",
        ConsoleColor.Red
      );

      List<string> result = new List<string>();

      Set<Token> current = new Set<Token>(
        Token.Identifier,
        Token.Comma
      );

      bool done = false;

      do
      {
        if (t.CurrentToken == Token.Identifier)
        {
          result.Add(t.CurrentString);
					position.Add(new Gezel.Model.CompoundPosition(t.CurrentStartPosition, t.CurrentEndPosition));
        }

        t.Accept(Token.Identifier, follow.Union(current));

        t.Check(follow.Union(current));

        if (t.CurrentToken == Token.Comma)
          t.Accept(Token.Comma, follow.Union(current));
        else
          done = true;
      }
      while (!done);

      ParserDebug.WriteLine(
        "Leaving 'ParseList'",
        ConsoleColor.Yellow
      );

      return result;
    }
  }
}
